package com.tianci.appstore.utils;

import java.util.ArrayList;
import java.util.LinkedList;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * <p>Description:</p>
 * <p>write something</p>
 *
 * @author GuoShangfeng
 * @version V*.*.*
 * @ClassName HLPositionUtil
 * @date 2014-7-16
 */
public class HLPositionUtil {
    public static class HLPosition {
        public int start = 0;
        public int end = -1;
    }

    public static HLPosition getResult2(String filter, String chinese) {
        char[] arr = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        char[] des = filter.toLowerCase().toCharArray();

        LinkedList<String> desQueue1 = new LinkedList<String>();
        for (char d : des) {
            System.out.print("" + d + "");
            desQueue1.add(String.valueOf(d));
        }

        LinkedList<String> desQueue = (LinkedList<String>) desQueue1.clone();

        System.out.println("");

        ArrayList<Integer> result = new ArrayList<Integer>();

        for (int i = 0; i < arr.length && desQueue.size() > 0; i++) {
            boolean has = false;
            String target = desQueue.peekFirst();
            System.out.print("[" + i + "] ----------- " + "target --->" + target
                    + "---- from -----" + arr[i] + "{---");
            String[] temp = null;
            if (arr[i] > 128) {
                try {
                    temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                String t = String.valueOf(arr[i]).toLowerCase();
                if (t.equals(" ")) {
                    continue;
                }
                temp = new String[]{t};
            }

            if (temp != null) {
                for (String s : temp) {
                    if (s.startsWith(target)) {
                        desQueue.pop();
                        has = true;
                        break;
                    }
                    System.out.print(" [" + s + "] ");
                }
            }
            System.out.println("---}");
            if (has) {
                result.add(i);
            } else if (desQueue.size() > 0) {
                result.clear();
                desQueue = (LinkedList<String>) desQueue1.clone();

                System.out.println(desQueue.size());
                if (temp != null) {
                    for (String s : temp) {
                        if (s.startsWith(target)) {
                            desQueue.pop();
                            has = true;
                            break;
                        }
                        System.out.print(" [" + s + "] ");
                    }
                }
                System.out.println("---}");
                if (has) {
                    result.add(i);
                }
            }
        }

        HLPosition hl = new HLPosition();
        if (result.size() > 0) {
            hl.start = result.get(0);
            hl.end = result.get(result.size() - 1);
        }
        return hl;
    }
}
